import emitter from "./emitter";
import playerDataInfo from "./playerData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    private nodePlane: cc.Node = null;
    private movePosi = cc.v2(0, 0);
    @property(cc.Prefab)
    enemyPlaneArr: cc.Prefab[] = [];
    @property(cc.Node)
    nodeEnemyPlane: cc.Node = null;
    enemyPlaneIndex = 5;//敌机波次

    enemyPlaneNumber = 0;//敌机数量 可累加
    wipePlane = 0;
    @property(cc.Prefab)
    pfGoods: cc.Prefab = null;

    private size = null;
    onLoad() {
        cc.director.getCollisionManager().enabled = true;
        this.addTouchListener();
        this.enemyPlaneByLevel();
        this.addEventListener();
        this.scheduleLoadEnemyPlane();
        this.createGoods();

        this.size = {
            height: -Math.round(cc.winSize.height / 2),//屏幕高度
            width: -Math.round(cc.winSize.width / 2),//屏幕宽度
            //四周最大边距
            maxWidth: Math.abs(Math.round(cc.winSize.width / 2)),
            minWidth: -Math.round(cc.winSize.width / 2),
            maxHeight: Math.abs(Math.round(cc.winSize.height / 2) - this.nodePlane.height / 2),
            minHeight: -Math.round(cc.winSize.height / 2) + this.nodePlane.height / 2,
        };
    }

    createGoods() {
        this.schedule(() => {
            let goods = cc.instantiate(this.pfGoods);
            goods.parent = this.nodeEnemyPlane;
            let size = cc.winSize;
            let fangXiang = Math.random();
            let randPosi = Math.random() * (size.width / 2) - 80;
            let posi = fangXiang > 0.5 ? randPosi : -randPosi;
            goods.setPosition(cc.v3(posi, (size.height / 2) + 100));
        }, 3)
    }

    addTouchListener() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStartCallback, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMoveCallback, this);
    }

    addEventListener() {
        emitter.on("setEnemyPlaneNumber", this.setEnemyPlaneNumber.bind(this));
        emitter.on("resetPlayerPosi", this.getPlayerPosi.bind(this));
        emitter.on("createRedJi", this.createRedJi.bind(this));
        emitter.on("stopMove", this.changeMoveState.bind(this));
        emitter.on("createPlane", this.createEnemyPlane.bind(this))
        emitter.on("bossDie", this.bossDie.bind(this))
    }


    onDisable() {
        emitter.off("setEnemyPlaneNumber", this.setEnemyPlaneNumber.bind(this));
        emitter.off("resetPlayerPosi", this.getPlayerPosi.bind(this));
        emitter.off("createRedJi", this.createRedJi.bind(this));
        emitter.off("stopMove", this.changeMoveState.bind(this));
        emitter.off("createPlane", this.createEnemyPlane.bind(this))
        emitter.off("bossDie", this.bossDie.bind(this))
    }

    //获取玩家最新位置
    getPlayerPosi(callback) {
        if (!callback) return
        let posi = this.nodePlane.parent.convertToWorldSpaceAR(this.nodePlane.position)
        callback(posi);
    }

    onTouchStartCallback(event) {
        this.movePosi = event.getLocation();
    }

    changeMoveState(state) {
        this.isMove = state;
    }

    isMove = true;;
    onTouchMoveCallback(event) {
        if (!this.isMove) return
        let movePosi = event.getLocation();
        let x = movePosi.x - this.movePosi.x;
        let y = movePosi.y - this.movePosi.y;
        this.movePosi = movePosi;

        if (this.nodePlane.x + x > this.size.maxWidth) this.nodePlane.x = this.size.maxWidth;
        else if (this.nodePlane.x + x < this.size.minWidth) this.nodePlane.x = this.size.minWidth;
        else this.nodePlane.x += x;

        if (this.nodePlane.y + y > this.size.maxHeight) this.nodePlane.y = this.size.maxHeight;
        else if (this.nodePlane.y + y < this.size.minHeight) this.nodePlane.y = this.size.minHeight;
        else this.nodePlane.y += y;
    }

    private scheduleCallback;
    //10秒生成一波敌机
    scheduleLoadEnemyPlane() {
        this.scheduleCallback = () => {
            this.enemyPlaneIndex -= 1;
            if (this.enemyPlaneIndex == 0) {
                // //加载boss    
                this.createEnemyBoos();
                this.isBoos = 1;
                emitter.emit("gameStart", false);
            }
            else {

                this.enemyPlaneByLevel();
            }
        }
        this.scheduleOnce(this.scheduleCallback.bind(this), 10);
    }

    //根据等级计算敌机数量及种类
    enemyPlaneByLevel() {
        if (this.isBoos == 2) return
        //@ts-ignore
        let level = playerDataInfo.gameData.level;
        let maxEnemyPlaneIndex = level * 2 + 1;
        this.enemyPlaneNumber += maxEnemyPlaneIndex;
        let maxEnemyPlaneNum = level * 3 - 1;
        if (maxEnemyPlaneNum > this.enemyPlaneArr.length) maxEnemyPlaneNum = this.enemyPlaneArr.length;
        this.createEnemyPlane(maxEnemyPlaneIndex, maxEnemyPlaneNum, 0, null);
        emitter.emit("setPlayerPosi", this.nodePlane.parent.convertToWorldSpaceAR(this.nodePlane.position));
    }

    //生成敌机
    createEnemyPlane(index: number, number: number, blood, posi) {
        for (let i = 0; i < index; i++) {
            if (i == index - 1) this.isCreate = false;
            this.scheduleCreatePlane(number, i, blood, posi);
        }
    }

    //延时生成
    scheduleCreatePlane(number: number, index: number, blood, nodePosi) {
        let self = this;
        this.scheduleOnce(() => {
            let rand = Math.round(Math.random() * number);
            if (rand == 6) {
                self.createQinWa();
            }
            else if (rand == 8) rand = 7;
            let enemyPlane = cc.instantiate(self.enemyPlaneArr[rand]);
            enemyPlane.parent = self.nodeEnemyPlane;
            let size = cc.winSize;
            let fangXiang = Math.random();
            let randPosi = Math.random() * (size.width / 2) - 80;
            let posi = fangXiang > 0.5 ? randPosi : -randPosi;

            if (blood != 0) {
                let planeItem = enemyPlane.getComponent("enemyPlaneUI");
                planeItem.setBloodProgress(blood, false);
                enemyPlane.setPosition(nodePosi);
            }

            //根据敌机种类设置位置
            switch (rand) {
                case 0:
                case 1:
                case 3:
                case 4:
                case 9:
                    enemyPlane.setPosition(cc.v2(posi, nodePosi != null ? nodePosi.y : (size.height / 2) + 100));
                    break;
                case 2:
                case 7:
                    //@ts-ignore
                    let ran = Math.round(Math.random() * 100) + 100;
                    let plane_2 = enemyPlane.getComponent("enemyPlane_2");
                    plane_2.setNodePosi(nodePosi != null ? nodePosi.y : (size.height / 2) - ran);
                    break
                case 5:
                case 10:
                    //@ts-ignore
                    let ran = Math.round(Math.random() * 100) + 100;
                    enemyPlane.setPosition(cc.v2(posi, nodePosi != null ? nodePosi.y : (size.height / 2) - ran));
                    break;
            }
            emitter.emit("setPlayerPosi", this.nodePlane.parent.convertToWorldSpaceAR(this.nodePlane.position));
        }, index * 0.01)
    }

    //分裂鸡死后生成红色小鸡
    createRedJi(posi) {
        if (this.isBoos == 2) return
        this.enemyPlaneNumber += 3;
        for (let i = 0; i < 3; i++) {
            let enemyPlane = cc.instantiate(this.enemyPlaneArr[1]);
            enemyPlane.parent = this.nodeEnemyPlane;
            let size = cc.winSize;
            let fangXiang = Math.random();
            let randPosi = Math.random() * (size.width / 2) - 80;
            let x = fangXiang > 0.5 ? randPosi : -randPosi;
            enemyPlane.setPosition(cc.v2(x, posi.y));
            let planeItem = enemyPlane.getComponent("enemyPlaneUI");
            planeItem.setBloodProgress(3 / planeItem.blood, false);
            emitter.emit("setPlayerPosi", this.nodePlane.parent.convertToWorldSpaceAR(this.nodePlane.position));
        }
    }

    createQinWa() {
        if (this.isBoos == 2) return
        this.enemyPlaneNumber += 2;
        for (let i = 0; i < 2; i++) {
            let enemyPlane = cc.instantiate(this.enemyPlaneArr[6]);
            enemyPlane.parent = this.nodeEnemyPlane;
            let size = cc.winSize;
            let fangXiang = Math.random();
            let randPosi = Math.random() * (size.width / 2) - 80;
            let x = fangXiang > 0.5 ? randPosi : -randPosi;
            enemyPlane.setPosition(cc.v2(x, (size.height / 2) + 100));
        }
    }

    //计算敌机数量  如果全部清空就生成敌机并且重置倒计时
    isCreate = false;
    isBoos = 0;// 0 boss未出现  1 boss出来了  2 boss死了
    setEnemyPlaneNumber() {
        this.enemyPlaneNumber -= 1;
        if (this.isBoos == 2 && this.enemyPlaneNumber <= 0) {
            emitter.emit("gameOver", "win");
            return
        }
        console.log("number ---------------------------------", this.enemyPlaneNumber);
        if (this.isCreate || this.enemyPlaneIndex <= 0) return;
        this.wipePlane += 1;
        if (this.enemyPlaneNumber == 0) {
            this.enemyPlaneIndex -= 1;
            this.unschedule(this.scheduleCallback);
            if (this.enemyPlaneIndex == 0) {
                // //加载boss    
                this.createEnemyBoos();
                this.isBoos = 1;
                emitter.emit("gameStart", false);
                this.scheduleLoadEnemyPlane();
            }
            else {
                this.isCreate = true;
                this.enemyPlaneByLevel();
                this.scheduleLoadEnemyPlane();
            }
        }
    }

    createEnemyBoos() {
        console.log("createEnemyBoos-----------------");
        //@ts-ignore
        let level = playerDataInfo.gameData.level
        if (level - 1 < this.enemyPlaneArr.length) {
            if (level - 1 == 8) level = 8;
            let enemyPlane = cc.instantiate(this.enemyPlaneArr[level - 1]);
            enemyPlane.parent = this.nodeEnemyPlane;
            let planeItem = enemyPlane.getComponent("enemyPlaneUI");
            planeItem.planeIsBoos(true);
            enemyPlane.setScale(1, 1);
            let size = cc.winSize;
            let fangXiang = Math.random();
            let randPosi = Math.random() * (size.width / 2) - 80;
            let x = fangXiang > 0.5 ? randPosi : -randPosi;
            //根据敌机种类设置位置
            switch (level - 1) {
                case 0:
                case 1:
                case 3:
                case 4:
                case 9:
                    enemyPlane.setPosition(cc.v2(x, (size.height / 2) + 100));
                    break;
                case 2:
                case 7:
                    //@ts-ignore
                    let ran = Math.round(Math.random() * 100) + 100;
                    let plane_2 = enemyPlane.getComponent("enemyPlane_2");
                    plane_2.setNodePosi((size.height / 2) - ran);
                    break
                case 5:
                case 10:
                    //@ts-ignore
                    let ran = Math.round(Math.random() * 100) + 100;
                    enemyPlane.setPosition(cc.v2(x, (size.height / 2) - ran));
                    break;
            }
            // enemyPlane.setPosition(cc.v2(x, (size.height / 2) + 100));
            emitter.emit("setPlayerPosi", this.nodePlane.parent.convertToWorldSpaceAR(this.nodePlane.position));
        }
    }

    bossDie() {
        this.isBoos = 2;
        this.unschedule(this.scheduleCallback);
    }
}
